package com.dy.design_pattern_study.abstract_factory_pattern.dome_abstract_factory;

import com.dy.design_pattern_study.abstract_factory_pattern.Department;
import com.dy.design_pattern_study.abstract_factory_pattern.User;
import com.dy.design_pattern_study.abstract_factory_pattern.dome_abstract_factory.IDB.IDepartment;
import com.dy.design_pattern_study.abstract_factory_pattern.dome_abstract_factory.IDB.IUser;
import com.dy.design_pattern_study.abstract_factory_pattern.dome_abstract_factory.dome_simple_factory.BaseDBReflex;
import com.dy.design_pattern_study.abstract_factory_pattern.dome_abstract_factory.dome_simple_factory.BaseDBSimple;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-04-05
 * @Content 优化抽象工厂
 *  这里使用简单工厂 + 反射来进行优化
 *  简单工厂可以使得调用抽象工厂更为简单
 *  使用反射则可以 去掉 简单工厂中繁琐的 Switch结构
 *
 *  因为我们知道这个抽象工厂模式如果需要添加不同的商品就需要分别修改抽象工厂的内容，
 *  并且在调用的时候，非常麻烦，需要分别的调用不同的抽象工厂，
 *  因此还可以对抽象工厂进行一个优化升级，就是多加一个简单工厂，去优化具体的抽象工厂的调用
 */
public class clientSimple {

    public static void main(String[] args) {

        simpleFactoryReflex();

        //simpleFactory();
    }

    /**
     * 利用反射来 去掉 Switch 选择
     */
    private static void simpleFactoryReflex() {
        User user = new User();
        Department department = new Department();
        for (IUser iu : BaseDBReflex.createUser()) {
            iu.Insert(user);
            iu.getUser(1);

        }
        System.out.println("--------------------------------");

        for (IDepartment idm : BaseDBReflex.createDepartment()) {
            idm.Insert(department);
            idm.getDepartment(1);
        }
    }

    /**
     * 简单工厂 -Switch结构
     */
    private static void simpleFactory() {
        User user = new User();
        Department department = new Department();

        IUser iu = BaseDBSimple.createUser();
        iu.Insert(user);
        iu.getUser(1);

        IDepartment idm = BaseDBSimple.createDepartment();
        idm.Insert(department);
        idm.getDepartment(1);
    }
}
